<?php

namespace app\models;

use Yii;
use yii\behaviors\AttributeBehavior;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "node".
 *
 * @property int $id
 * @property int $name 节点名称
 * @property int $sort 排序
 * @property int $parent_id 父级节点ID
 * @property int $created_at
 * @property int $updated_at
 */
class Node extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%node}}';
    }

    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            ['name', 'required','on'=>['create','edit']],
            ['name', 'unique','on'=>['create']],
            ['name', 'unique','on'=>['edit'],'filter'=>['<>','id',$this->id]],
            ['sort', 'integer','on'=>['create','edit']],
            ['parent_id','safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'name' => '节点名称',
            'sort' => '排序号',
        ];
    }

    public function getProject()
    {
        return $this->hasMany(Project::className(),['node_id'=>'id']);
    }


}
